<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_payment_group Extends MY_Model
{

	public function __construct()
	{
		parent::__construct();
	
	}
	
	public function set_group($data)
	{
		unset($data['create_payment_group']);
		$this->db->insert('payment_group',$data);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function get_group($id = FALSE)
	{
		if($id == FALSE)
		{
			$q = $this->db->select('id,group_name,group_desc')->get('payment_group');
			
			if($q->num_rows() >= 1)
			{
				
				foreach($q->result() as $k => $v)
				{
					$data[$k]['group_name'] = $v->group_name;
					$data[$k]['group_desc'] = $v->group_desc;
					$data[$k]['id'] = $v->id;
					$data[$k]['grade_levels'] = $this->_get_grade_levels($v->id);
				}
				
				return $data;
			}else{
				return FALSE;
			}
			
		}else{
			$q = $this->db->select('id,group_name,group_desc')->where('id',$id)->get('payment_group');
			
			if($q->num_rows() >= 1)
			{
			
				foreach($q->result() as $v)
				{
					$data['group_name'] = $v->group_name;
					$data['group_desc'] = $v->group_desc;
					$data['id'] = $v->id;
					$data['grade_levels'] = $this->_get_grade_levels($v->id);
				}
				
				return $data;
			}else{
				return FALSE;
			}
		}
	

	}
	
	private function _get_grade_levels($id)
	{
		$q = $this->db->select('level_code,level_desc')
					->where('payment_group',$id)
				 ->get('levels');
		
		return $q->num_rows() >= 1 ? $q->result() : NULL;
	}
	
	public function get_array($type)
	{
		$this->db->select('fee_id,fee_name,fee_desc,fee_rate');

		if($type == 'tuit')
		{
			$this->db->where('is_tuition_fee',1);
		}elseif($type== 'misc')
		{
			$this->db->where('is_misc_fee',1);
		}elseif($type == 'cca')
		{
			$this->db->where('is_cca_fee',1);
		}
		
		// $q= $this->db->where('fee_stat','active')
				 // ->get('fees');
				 
		 $q= $this->db->get('fees');
		
		
		// pd($this->db->last_query());
		$select[''] = 'Select Fee';
		
		if($q->num_rows() >= 1)
		{
			$t = $q->result();
			
			foreach($t as $k => $v)
			{
				$select[$v->fee_id] = $v->fee_name .' | '. $v->fee_desc . ' | '.$v->fee_rate;
			}
			
			return $select;
		}else{
			return $select;
		}
	}
	
	public function get_selected_grade_level($id)
	{
		$select = 'SELECT level_id,level_desc,level_code
					FROM levels 
					where payment_group = ?';
					
		$q = $this->db->query($select,array($id));
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	}
	
	public function get_unselected_grade_level()
	{
		$select = 'SELECT level_id,level_desc,level_code
					FROM levels 
					where payment_group = 0';
					
		$q = $this->db->query($select);
		
		return $q->num_rows() >= 1 ? $q->result() : FALSE;
	
	}
	
	public function create_payment_group_fees($data,$id)
	{
		unset($data['create_payment_group_fees']);
		// pd($data);
		$fee_id = implode(',',array_values($data));
				
		$select = 'SELECT * FROM fees 
					WHERE fee_id in ('.$fee_id.')';
		$q = $this->db->query($select);
		
		if($q->num_rows() >= 1)
		{
			foreach($q->result() as $k => $v)
			{
				if($v->is_misc_fee == 1)
				{
					$fee[$k]['fee_type'] = 'misc';
				}elseif($v->is_tuition_fee == 1){
				
					$fee[$k]['fee_type'] = 'tuit';
				}elseif($v->is_cca_fee == 1)
				{
					$fee[$k]['fee_type'] = 'cca';
				}
				
				$fee[$k]['group_id'] = $id;
				$fee[$k]['fee_name'] = $v->fee_name;
				$fee[$k]['fee_price'] = $v->fee_rate;
				$fee[$k]['fee_id'] = $v->fee_id;
				$fee[$k]['fee_desc'] = $v->fee_desc;
				$fee[$k]['sy_id'] = $this->c_sy->id;
				$fee[$k]['gp_id'] = $this->c_gp->gp_id;
				$fee[$k]['created'] = NOW;
			}
			
				$this->db->insert_batch('payment_group_fees',$fee);
				$this->_generate_payment_mode_for_group($id);
				return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		}
	}
	
	private function _generate_payment_mode_for_group($id)
	{

		$select = $this->db->select()->where('id',$id)->limit(1)->get('payment_group');
		
		if($select->num_rows() >= 1)
		{	
			$q = $select->row();
			$brands = array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
			$control = array('monthly','annual','semestral','trimester');
			$month = array('January','February','March','April','May','June','July','August','September','October','November','Registration');
			$x=0;
			
			foreach($control as $con)
			{
				foreach($brands as $bk => $br)
				{
					$code = preg_replace('~[^a-zA-Z0-9]+~', '', trim(strtolower($q->group_name)));
					$for_payment_modes[$x]['setting_name'] = $con.'_'.$code.'_'.$br;
					$for_payment_modes[$x]['setting_value'] = 0.00;
					$for_payment_modes[$x]['setting_group'] = $code;
					$for_payment_modes[$x]['level_id'] = $id;
					$for_payment_modes[$x]['payment_mode_name'] = $con;
					$for_payment_modes[$x]['month_name'] = $month[$bk];
					$x++;
				}
			}
			
			
			// pd($for_payment_modes);
			$this->db->insert_batch('payment_modes',$for_payment_modes);
			return $this->db->affected_rows() >=1 ? $R_TRUE : $R_FALSE;
			
		}else{
		

		}
	
	
	}
	
	
	public function set_grade_levels($data,$id)
	{
		

		$this->db->set('payment_group',$id)
				 ->where_in('level_id',$data['levels'])
				 ->update('levels');
		
		
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
			
	}
	
	
	
	public function get_set_fee($type = '',$id)
	{
		$q = $this->db->select('concat(fee_name," ",fee_desc," ",fee_price) as descs,id',FALSE)
					  ->where('group_id',$id)
					  ->where('fee_type',$type)
					  ->get('payment_group_fees');
	
		return $q->num_rows() >= 1 ? $q->row() : FALSE; 
	}
	
	public function remove_level_from_group($id)
	{
		$this->db->set('payment_group',0)
			 ->where('level_id',$id)
			 ->update('levels');
			 
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function reset_group_fees($id)
	{
		$this->db->where('group_id',$id)->delete('payment_group_fees');
		$this->db->where('level_id',$id)->delete('payment_modes');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function show_payment_group_fees()
	{
		$q = $this->db->select('group_name,id')->get('payment_group');
		return $q->num_rows() >= 1 ? $q->result_array() : FALSE;
	}
	
	public function total_fee_for_group($id)
	{
		$q = $this->db->select('sum(fee_price) as total')
					  ->where('group_id',$id)
					  ->get('payment_group_fees');
					  
		return $q->num_rows() >= 1 ? $q->row()->total : FALSE;

	}

}